package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO 购物车数据
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 1、根据用户id和菜品或套餐id或口味查询购物车, 得到购物车列表()
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId()); // 通过ThreadLocal设置当前用户id
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        // 2、判断当前菜品或套餐是否在购物车中
        if (list != null && !list.isEmpty()) {
            // 3、如果已经存在，数量加1
            ShoppingCart cart = list.get(0); // 获取购物车对象 -- 在当前条件下, 查到的购物车对象只能有一个
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart); // 肯定包括id, 因为是在数据库中查出来的
        } else {
            // 4、如果不存在，完善数据后添加到购物车
            // 4.1 判断添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                // 添加的是菜品
                Dish dish = dishMapper.getById(dishId);
                // 4.2 完善菜品数据
                shoppingCart.setName(dish.getName()); // 菜品名称
                shoppingCart.setImage(dish.getImage()); // 图片
                shoppingCart.setAmount(dish.getPrice()); // 价格
            } else {
                // 添加的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                // 4.2 完善菜品数据
                shoppingCart.setName(setmeal.getName()); // 套餐名称
                shoppingCart.setImage(setmeal.getImage()); // 图片
                shoppingCart.setAmount(setmeal.getPrice()); // 价格
            }
            // 4.2 完善数据
            shoppingCart.setNumber(1); // 数量
            shoppingCart.setCreateTime(LocalDateTime.now()); // 创建时间
            // 4.3 添加到数据库
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return 购物车列表
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId(); // 只查当前用户的购物车
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
